package com.cax.module.enterprise.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cax.common.constant.Constants;
import com.cax.module.enterprise.domain.dto.EnterpriseCertificateQueryDTO;
import com.cax.module.enterprise.domain.dto.EnterpriseCertificateUpdateDTO;
import com.cax.module.enterprise.mapper.EnterpriseCertificateMapper;
import com.cax.module.enterprise.service.EnterpriseCertificateService;
import com.cax.module.file.domain.FileType;
import com.cax.module.file.domain.vo.File2VO;
import com.cax.module.file.service.FileService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.cax.module.enterprise.domain.vo.EnterpriseCertificateVO;
import com.cax.module.enterprise.domain.EnterpriseCertificate;
import cn.hutool.core.bean.BeanUtil;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 企业证书表 Service实现类
 * @author makejava
 * @date 2024-08-22 17:25:18
 */
@Service
@Slf4j
public class EnterpriseCertificateServiceImpl extends ServiceImpl<EnterpriseCertificateMapper, EnterpriseCertificate> implements EnterpriseCertificateService {



	@Autowired
	private  FileService fileService;

	@Override
	public List<EnterpriseCertificateVO> getByName(String name){
		List<EnterpriseCertificateVO> result = new ArrayList<>();
		if(StrUtil.isBlank(name)){
			return result;
		}

		LambdaQueryWrapper<EnterpriseCertificate> lqw = new LambdaQueryWrapper<>();
		lqw.in(EnterpriseCertificate::getName,name);
		lqw.in(EnterpriseCertificate::getDeleteFlag,Constants.NO);
		List<EnterpriseCertificate> list = this.list(lqw);
		if (CollUtil.isEmpty(list)) {
			return result;
		}

		result = BeanUtil.copyToList(list, EnterpriseCertificateVO.class);
		List<Integer> ids = result.stream()
				.map(i -> i.getId())
				.collect(Collectors.toList());
		Map<Integer, List<File2VO>> fileMap = fileService.getMap(FileType.TypeEnum.FILE_TYPE_3.getCode(), ids);
		for(EnterpriseCertificateVO i:result){
			if(fileMap.containsKey(i.getId())&&CollUtil.isNotEmpty(fileMap.get(i.getId()))){
				i.setFileUrl(fileMap.get(i.getId()).get(0).getAccessUrl());
			}
		}

		return result;
	}


	@Override
    public Page<EnterpriseCertificateVO> getPage(EnterpriseCertificateQueryDTO dto){
		//创建分页对象
		Page<EnterpriseCertificateVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
		List<EnterpriseCertificateVO> result = this.getBaseMapper().findEnterpriseCertificateList(page,dto);
		page.setRecords(result);
		return page;
	}

	/**
	 * 通过主键查询
	 */
	@Override
	public EnterpriseCertificateVO getByKey(Integer id){
		EnterpriseCertificate entity = super.getById(id);
		EnterpriseCertificateVO result = BeanUtil.copyProperties(entity, EnterpriseCertificateVO.class);
		List<File2VO> file2VOS = fileService.get(FileType.TypeEnum.FILE_TYPE_3.getCode(),id);
		if(!file2VOS.isEmpty()){
			result.setFileUrl(file2VOS.get(0).getAccessUrl());
		}
        return result;
    }

	/**
	 * 添加
	 */
	@Override
	public Integer add(EnterpriseCertificateUpdateDTO dto){
		EnterpriseCertificate enterpriseCertificate = BeanUtil.copyProperties(dto, EnterpriseCertificate.class);
		this.save(enterpriseCertificate);
		if(dto.getFileId() != null){
			fileService.addSave(enterpriseCertificate.getId(), Lists.newArrayList(dto.getFileId()));
		}
		return enterpriseCertificate.getId();
	}


	@Override
	public void upd(EnterpriseCertificateUpdateDTO dto){
		EnterpriseCertificate enterpriseCertificate = BeanUtil.copyProperties(dto, EnterpriseCertificate.class);
	    this.updateById(enterpriseCertificate);

		fileService.updSave(FileType.TypeEnum.FILE_TYPE_3.getCode(),enterpriseCertificate.getId(),Lists.newArrayList(dto.getFileId()));
	}



	@Override
	public void del(Integer id){
		this.updateById(new EnterpriseCertificate().setId(id).setDeleteFlag(Constants.YES).setUpdateTime(LocalDateTime.now()));
	}
}



